package com.mr.cai.main.httpApi;

import android.os.Bundle;

import com.cai.mr.lib.utils.LogInfo;
import com.cai.mr.lib.utils.OkHttpUtil;
import com.mr.cai.main.bean.base.MrCaiSubBaseBean;
import com.mr.cai.main.parse.base.MrCaiBaseParse;
import com.mr.cai.main.utils.MrCaiConstant;

import org.json.JSONObject;

import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by hai8108 on 16/2/5.
 */
public class MrCaiHttpApi {
    /**
     * 公共参数  KEY
     */
    public interface PUBLIC_PARAMETERS {
        String MOD_KEY = "uid";
        String PCODE_KEY = "pcode";
        String TOKEN = "token";
        String VERSION_KEY = "version";  //App版本号
        String PAGEINDEX_KEY = "pageindex";
        String PAGESIZE_KEY = "pagesize";
        String OS = "osType";
        String OS_VERSION = "osversion"; //操作系统版本号
        String OS_NAME = "os";  //操作系统名称
        String BRAND = "brand"; //手机品牌
    }

    /**
     * 正式接口
     */
    public interface OFFICIAL_URL {
        //http://123.56.187.74:8090/tvmall/tv/b2c/images/t_commodity_category/63_.jpg
        String IMAGE_URL_HEAD = "http://123.56.187.74:8090/tvmall";
        //http://123.56.187.74:8090/tvmall/customerTv/getAuthcode.action?
        String DYNAMIC_APP_URL = "http://123.56.187.74:8090/tvmall/customerTv/";
        //http://123.56.187.74:8090/tvmall/tv/commodity/listCategory.action?parentId=55
        String COMMODITY_APP_URL = "http://123.56.187.74:8090/tvmall/tv/commodity/";
        //http://localhost:8080/tvmall/shopcartTv/
        String SHOPCARTTV_APP_URL = "http://123.56.187.74:8090/tvmall/shopcartTv/";
    }

    public interface OFFICIAL_ACTION {
        //获取验证码接口
        String GET_AUTHCODE = "getAuthcode.action";
        //验证码校验接口
        String CHECK_AUTHCODE = "checkAuthCode.action";
        //客户注册接口
        String REGISTER_CUSTOMER = "registerCustomer.action";
        //重置密码接口
        String RESET_PASSWORD = "resetPassword.action";
        //客户登录接口
        String CHECK_PHONE_AND_PASSWORD = "checkPhoneAndPassword.action";
        //第三方平台账号登录接口
        String THIRD_LOGIN = "thirdLogin.action";

        //购物车列表获取
        String SHOP_CART_LIST = "shopcartList.action";
        //购物车选中、取消选中
        String SHOP_CHECKED_LIST = "checked.action";
        //更改购物车中商品的数量 减
        String SHOP_MINUS_LIST = "minus.action";
        //更改购物车中商品的数量 加 
        String SHOP_PLUS_LIST = "plus.action";
        //商品加入购物车
        String SHOP_ADDTOSHOPCART_LIST = "deleteFromShopcart.action";
        //删除购物车中的商品
        String SHOP_DELETEFROMSHOPCART_LIST = "deleteFromShopcart.action";
        // 分类列表
        String LIST_CATEGORY_LIST = "listCategory.action";
        //我的券
        String LIST_COUPON = "listCoupon.action";
    }

    public interface AUTH_CODE_PARAMETERS {
        String AUTH_CODE_PHONE = "phone";
        String PASSWORD = "password";
        String AUTHCODE = "authCode";
    }

    //===============================================================================
    //===============================================================================
    private static String getStaticHead () {
        return OFFICIAL_URL.DYNAMIC_APP_URL;
    }

    //===============================================================================
    private static <T extends MrCaiSubBaseBean> Observable<T> request (final String baseUrl,
                                                                       final Bundle params,
                                                                       final String endUrl,
                                                                       final MrCaiBaseParse<T>
                                                                               parser) {
        String realUrl = OkHttpUtil.attachHttpGetParams(baseUrl, params, endUrl);
        LogInfo.log("haitian", "requestUrl =" + realUrl);
        return Observable.just(realUrl)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, T>() {
                    @Override
                    public T call (String s) {
                        try {
                            return parser.parse(new JSONObject(OkHttpUtil.getStringFromServer(s)));
                        } catch (Exception e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                });
    }

    /**
     * 获取注册验证码
     *
     * @param phoneNum
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestAuthcode (final String phoneNum,
                                                                              final MrCaiBaseParse<T> parser) {
        String baseUrl = getStaticHead() + OFFICIAL_ACTION.GET_AUTHCODE;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(AUTH_CODE_PARAMETERS.AUTH_CODE_PHONE, phoneNum);
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     * 注册
     *
     * @param phoneNum
     * @param pwd
     * @param authCode
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestRegister (final String phoneNum,
                                                                              final String pwd,
                                                                              final String authCode,
                                                                              final MrCaiBaseParse<T> parser) {
        //http://123.56.187.74:8090/tvmall/customerTv/registerCustomer.action?phone=170010901226&password=123456&authCode=789373
        String baseUrl = getStaticHead() + OFFICIAL_ACTION.REGISTER_CUSTOMER;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(AUTH_CODE_PARAMETERS.AUTH_CODE_PHONE, phoneNum);
        params.putString(AUTH_CODE_PARAMETERS.PASSWORD, pwd);
        params.putString(AUTH_CODE_PARAMETERS.AUTHCODE, authCode);
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     * 验证短信验证码
     *
     * @param phoneNum
     * @param authCode
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestMsgCode (final String phoneNum,
                                                                             final String authCode,
                                                                             final MrCaiBaseParse<T> parser) {
        //http://123.56.187.74:8090/tvmall/customerTv/checkAuthCode.action?phone=17801090192&authCode=936410
        String baseUrl = getStaticHead() + OFFICIAL_ACTION.CHECK_AUTHCODE;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(AUTH_CODE_PARAMETERS.AUTH_CODE_PHONE, phoneNum);
        params.putString(AUTH_CODE_PARAMETERS.AUTHCODE, authCode);
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     * 密码设置
     *
     * @param phoneNum
     * @param pwd
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestResetPwd (final String phoneNum,
                                                                              final String pwd,
                                                                              final MrCaiBaseParse<T> parser) {
        //http://123.56.187.74:8090/tvmall/customerTv/resetPassword
        // .action?phone=17801090192&password=936410
        String baseUrl = getStaticHead() + OFFICIAL_ACTION.RESET_PASSWORD;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(AUTH_CODE_PARAMETERS.AUTH_CODE_PHONE, phoneNum);
        params.putString(AUTH_CODE_PARAMETERS.PASSWORD, pwd);
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     * 登录
     *
     * @param phoneNum
     * @param pwd
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestLogin (final String phoneNum,
                                                                           final String pwd,
                                                                           final MrCaiBaseParse<T> parser) {
        //http://123.56.187.74:8090/tvmall/customerTv/checkPhoneAndPassword.action?phone=17801090192&password=936410
        String baseUrl = getStaticHead() + OFFICIAL_ACTION.CHECK_PHONE_AND_PASSWORD;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(AUTH_CODE_PARAMETERS.AUTH_CODE_PHONE, phoneNum);
        params.putString(AUTH_CODE_PARAMETERS.PASSWORD, pwd);
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     * 分类数据请求
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestGroupList (final MrCaiBaseParse<T> parser) {
        String baseUrl = OFFICIAL_URL.COMMODITY_APP_URL + OFFICIAL_ACTION.LIST_CATEGORY_LIST;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString("parentId", "119");
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    public static <T extends MrCaiSubBaseBean> Observable<T> requestSearchHotWordList (final
                                                                           MrCaiBaseParse<T> parser) {
        /**
         * 获取推荐搜索词
         http://localhost:8080/tvmall/tv/commodity/listKeyWords.action
         {"result":true,"data":"赣南脐橙|栖霞苹果 |绿控黄瓜","code":0,"msg":"success"}

         */
        String baseUrl = OFFICIAL_URL.COMMODITY_APP_URL + "listKeyWords.action";
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     * 接口描述	根据客户手机号查询购物车信息
     *
     * @param customerId
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestCartList (final String customerId,
                                                                              final MrCaiBaseParse<T> parser) {
        /**
         * 查询购物车
         接口描述	根据客户手机号查询购物车信息
         请求值	http://localhost:8080/tvmall/shopcartTv/shopcartList.action?customerId=fbb83bca52a663a50152a6f230d50006
         成功返回值	{"result":true,"code":0,"data":[{"id":"2c908bd34261984f014261a012a70009","commodityId":"2c9094833a44271e013a4852f94902a4","stockNo":"S101106","checked":1,"count":1,"name":"INTEX豪华充气双人床","presentPrice":"298.00"}],"msg":"success"}
         失败返回值	{"result":false,"code":-1,"data":"","msg":"error"}
         */
        String baseUrl = OFFICIAL_URL.SHOPCARTTV_APP_URL + OFFICIAL_ACTION.SHOP_CART_LIST;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString("customerId", customerId);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     * 购物车选中、取消选中
     * @param customerId
     * @param shopcartId
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestCartChecked (final String
                                                                                         customerId,
                                                                                 final String
                                                                                         shopcartId,
                                                                                 final MrCaiBaseParse<T> parser) {
        /**
         购物车选中
         接口描述	购物车选中、取消选中
         请求值	http://localhost:8080/tvmall/shopcartTv/checked.action?shopcartId=2c908bd34261984f014261a012a70009
         成功返回值	{"result":true,"code":0,"data":{"checked":0},"msg":"success"}
         失败返回值	{"result":false,"code":-1,"data":"","msg":"error"}
         */
        String baseUrl = OFFICIAL_URL.SHOPCARTTV_APP_URL + OFFICIAL_ACTION.SHOP_CHECKED_LIST;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString("customerId", customerId);
        params.putString("shopcartId", shopcartId);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     * 更改购物车中商品的数量 减
     * @param customerId
     * @param shopcartId
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestCartMinus (final String
                                                                                         customerId,
                                                                                 final String
                                                                                         shopcartId,
                                                                                 final MrCaiBaseParse<T> parser) {
        /**
         更改购物车中商品的数量 减
         接口描述	更改购物车中商品的数量减
         请求值	http://localhost:8080/tvmall/shopcartTv/minus.action?shopcartId=40285d8152de57a10152de677d6d0001
         成功返回值	{"result":true,"code":0,"data":"","msg":"success"}
         失败返回值	{"result":false,"code":-1,"data":"","msg":"error"}
         */
        String baseUrl = OFFICIAL_URL.SHOPCARTTV_APP_URL + OFFICIAL_ACTION.SHOP_MINUS_LIST;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString("customerId", customerId);
        params.putString("shopcartId", shopcartId);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     *更改购物车中商品的数量加
     * @param customerId
     * @param shopcartId
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestCartPlus (final String
                                                                                         customerId,
                                                                                 final String
                                                                                         shopcartId,
                                                                                 final MrCaiBaseParse<T> parser) {
        /**
         更改购物车中商品的数量 加
         接口描述	更改购物车中商品的数量加
         请求值	http://localhost:8080/tvmall/shopcartTv/plus.action?shopcartId=40285d8152de57a10152de677d6d0001
         成功返回值	{"result":true,"code":0,"data":"","msg":"success"}
         失败返回值	{"result":false,"code":-1,"data":"","msg":"error"}
         */
        String baseUrl = OFFICIAL_URL.SHOPCARTTV_APP_URL + OFFICIAL_ACTION.SHOP_PLUS_LIST;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString("customerId", customerId);
        params.putString("shopcartId", shopcartId);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     * 商品加入购物车
     * @param customerId
     * @param commodityId
     * @param stockNo
     * @param count
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestCartAdd (final String
                                                                                         customerId,
                                                                                 final String
                                                                                         commodityId,
                                                                             final String stockNo,
                                                                             final String count,
                                                                                 final MrCaiBaseParse<T> parser) {
        /**
         加入购物车
         接口描述	商品加入购物车
         请求值	http://localhost:8080/tvmall/shopcartTv/addToShopcart.action?commodityId=40285d8152a77b420152a7868fdf0013&stockNo=A1000604&count=1&customerId=fbb83bca52a663a50152a6f230d50006
         成功返回值	{"result":true,"code":0,"data":"","msg":"success"}
         失败返回值	{"result":false,"code":-1,"data":"","msg":"error"}
         */
        String baseUrl = OFFICIAL_URL.SHOPCARTTV_APP_URL + OFFICIAL_ACTION.SHOP_ADDTOSHOPCART_LIST;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString("customerId", customerId);
        params.putString("commodityId", commodityId);
        params.putString("stockNo", stockNo);
        params.putString("count", count);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     * 删除购物车中的商品
     * @param customerId
     * @param shopcartId
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestCartDelete (final String
                                                                                         customerId,
                                                                                 final String
                                                                                         shopcartId,
                                                                                 final MrCaiBaseParse<T> parser) {
        /**
         接口描述	删除购物车中的商品
         请求值	http://localhost:8080/tvmall/shopcartTv/deleteFromShopcart.action?shopcartId=2c908bd34261984f014261a012a70009
         成功返回值	{"result":true,"code":1,"data":"","msg":"success"}
         失败返回值	{"result":false,"code":-1,"data":"","msg":"error"}
         */
        String baseUrl = OFFICIAL_URL.SHOPCARTTV_APP_URL + OFFICIAL_ACTION.SHOP_DELETEFROMSHOPCART_LIST;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString("customerId", customerId);
        params.putString("shopcartId", shopcartId);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }

    /**
     * 查询优惠劵列表
     * @param customerId
     * @param parser
     * @param <T>
     * @return
     */
    public static <T extends MrCaiSubBaseBean> Observable<T> requestTicketsList (final String
                                                                                         customerId,
                                                                               final MrCaiBaseParse<T> parser) {
        /**
         * http://localhost:8080/tvmall/couponTv/listCoupon.action?customerId=fbb83bca52a663a50152a6f230d50006&pageNo=1
         */
        String baseUrl = getStaticHead() + OFFICIAL_ACTION.LIST_COUPON;
        String endUrl = null;
        Bundle params = new Bundle();
        params.putString("customerId", customerId);
        params.putString("pageNo", "1");
        params.putString(PUBLIC_PARAMETERS.VERSION_KEY, MrCaiConstant.VERSION);
        params.putString(PUBLIC_PARAMETERS.OS, MrCaiConstant.OS_TYPE);
        return request(baseUrl, params, endUrl, parser);
    }
}
